package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javabean.Orders;
import com.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: 张波
 * @Time: 2025/8/27 19:03
 */
@Controller
@RequestMapping(("/orders"))
public class OrdersController {

	OrdersService ordersService;
	@Autowired
	public OrdersController(OrdersService ordersService) {
		this.ordersService = ordersService;
	}
	/// 查询所有数据测试
	@RequestMapping("/findAllDemo")
	@ResponseBody
	public List<Orders> findAll(){
		return ordersService.findAll();
	}
	/// 加入分页插件
	@RequestMapping("/findAll")
	public String findAll(
			HttpServletRequest request,
			@RequestParam(value = "page",defaultValue = "1")int page,
			@RequestParam(value = "limit",defaultValue = "5") int limit,
			@RequestParam(value = "key",required = false) String key,
			@RequestParam(value = "value",required = false) String value){
		String orderNum = "";
		String productName = "";
		if ("orderNum".equals(key)){
			orderNum = value;
		}else if ("productName".equals(key)){
			productName = value;
		}
		PageHelper.startPage(page,limit);
		List<Orders> all = ordersService.findAllByOrderNumAndProductName(orderNum, productName);
		PageInfo<Orders> pageInfo = new PageInfo<>(all);
		request.setAttribute("pageInfo",pageInfo);
		request.setAttribute("select",key);
		request.setAttribute("selectValue",value);
		System.out.println("key: " + key + ", value: " + value);
		return "/orders/orders-list";
	}

	@RequestMapping("/findById")
	public String findById(HttpServletRequest request,int id){
		Orders byId = ordersService.findById(id);
		request.setAttribute("order",byId);
		return "/orders/orders-show";
	}
}
